#pragma once
#ifndef _ASIO_SERVER_H_
#define _ASIO_SERVER_H_

#include "Netbase.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include "asio_NetworkHandler.h"
using namespace boost;
using namespace boost::asio;

namespace XGC
{
	namespace net
	{
		class asio_ServerBase
		{
		public:
			asio_ServerBase( _uint32 nTimerInvent = 0 );
			~asio_ServerBase(void);

			bool StartServer( const char *address, short port );
			void StopServer();

		protected:
			void handle_accept( asio_SocketPtr pSocket, const system::error_code& error);
			void handle_stop();

			virtual void post_accept() = 0;

		protected:
			io_service service_;
			ip::tcp::acceptor acceptor_;
			std::vector< shared_ptr< thread > >	workthreads_;
			unsigned int timer_invent;
			IMessageQueue *m_pMessageQueue;
		};

		class asio_Server : public asio_ServerBase
		{
		public:
			asio_Server( IMessageQueue *pMessageQueue, _uint32 nTimeout = 0 );
			~asio_Server();

		protected:
			virtual void post_accept();

		private:
			IMessageQueue*	m_pMessageQueue;
		};

		class asio_ServerEx	:	public asio_ServerBase
		{
		public:
			asio_ServerEx( create_handler_func call, long_ptr param, _uint32 timeout = 0 );
			~asio_ServerEx(void);

		protected:
			virtual void post_accept();

		private:
			create_handler_func call_;
			long_ptr param_;
		};

	}
}
#endif // _ASIO_SERVER_H_